package com.health.medical.service.impl;

import java.time.Duration;
import java.util.List;

import com.health.common.utils.DateUtils;
import com.health.common.utils.MinIOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.health.medical.mapper.MedicalDoctorMapper;
import com.health.medical.domain.MedicalDoctor;
import com.health.medical.domain.dto.DoctorDetailDTO;
import com.health.medical.service.IMedicalDoctorService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 医生管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-26
 */
@Service
public class MedicalDoctorServiceImpl implements IMedicalDoctorService {
    @Autowired
    private MedicalDoctorMapper medicalDoctorMapper;

    @Autowired
    private MinIOUtils minIOUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询医生管理
     *
     * @param doctorId 医生管理主键
     * @return 医生管理
     */
    @Override
    public MedicalDoctor selectMedicalDoctorByDoctorId(Long doctorId) {
        return medicalDoctorMapper.selectMedicalDoctorByDoctorId(doctorId);
    }

    /**
     * 查询医生详情（包含机构名和科室名）- 多表联查
     *
     * @param doctorId 医生管理主键
     * @return 医生详情
     */
    @Override
    public DoctorDetailDTO selectDoctorDetailByDoctorId(Long doctorId) {
        return medicalDoctorMapper.selectDoctorDetailByDoctorId(doctorId);
    }

    /**
     * 查询医生管理列表 - 单表查询
     *
     * @param medicalDoctor 医生管理
     * @return 医生管理
     */
    @Override
    public List<MedicalDoctor> selectMedicalDoctorList(MedicalDoctor medicalDoctor) {
        return medicalDoctorMapper.selectMedicalDoctorList(medicalDoctor);
    }

    /**
     * 查询医生详情列表（包含机构名和科室名）- 多表联查
     *
     * @param medicalDoctor 医生管理
     * @return 医生详情集合
     */
    @Override
    public List<DoctorDetailDTO> selectDoctorDetailList(MedicalDoctor medicalDoctor) {
        return medicalDoctorMapper.selectDoctorDetailList(medicalDoctor);
    }

    /**
     * 新增医生管理
     *
     * @param medicalDoctor 医生管理
     * @return 结果
     */
    @Override
    public int insertMedicalDoctor(MedicalDoctor medicalDoctor) {
        medicalDoctor.setCreateTime(DateUtils.getNowDate());
        return medicalDoctorMapper.insertMedicalDoctor(medicalDoctor);
    }

    /**
     * 修改医生管理
     *
     * @param medicalDoctor 医生管理
     * @return 结果
     */
    @Override
    public int updateMedicalDoctor(MedicalDoctor medicalDoctor) {
        medicalDoctor.setUpdateTime(DateUtils.getNowDate());
        return medicalDoctorMapper.updateMedicalDoctor(medicalDoctor);
    }

    /**
     * 批量删除医生管理
     *
     * @param doctorIds 需要删除的医生管理主键
     * @return 结果
     */
    @Override
    public int deleteMedicalDoctorByDoctorIds(Long[] doctorIds) {
        return medicalDoctorMapper.deleteMedicalDoctorByDoctorIds(doctorIds);
    }

    /**
     * 删除医生管理信息
     *
     * @param doctorId 医生管理主键
     * @return 结果
     */
    @Override
    public int deleteMedicalDoctorByDoctorId(Long doctorId) {
        return medicalDoctorMapper.deleteMedicalDoctorByDoctorId(doctorId);
    }

    /**
     * 上传医生头像
     */
    @Override
    public String uploadAvatar(MultipartFile file, Long doctorId) {
        String url;
        try {
            // 检查文件是否为空
            if (file == null || file.isEmpty()) {
                throw new RuntimeException("上传的文件为空");
            }

            if (file.getSize() == 0) {
                throw new RuntimeException("文件大小为0");
            }
            // 尝试上传
            System.out.println("调用MinIOUtils.uploadFile上传医生头像...");
            url = minIOUtils.uploadFile(file);
            System.out.println("医生头像上传成功，URL: " + url);
            // 查询数据库中老的头像URL，若存在则删除
            MedicalDoctor doctor = medicalDoctorMapper.selectMedicalDoctorByDoctorId(doctorId);
            if (doctor != null && doctor.getAvatar() != null && !doctor.getAvatar().trim().isEmpty()) {
                try {
                    System.out.println("删除旧医生头像: " + doctor.getAvatar());
                    minIOUtils.deleteFile(doctor.getAvatar());
                } catch (Exception e) {
                    // 删除旧文件失败不影响新文件上传，只记录日志
                    System.err.println("删除旧医生头像失败: " + e.getMessage());
                }
            }
            // 将新头像URL更新进数据库
            MedicalDoctor updateDoctor = new MedicalDoctor();
            updateDoctor.setDoctorId(doctorId);
            updateDoctor.setAvatar(url);
            updateDoctor.setUpdateTime(DateUtils.getNowDate());
            int updateResult = medicalDoctorMapper.updateMedicalDoctor(updateDoctor);
            System.out.println("医生头像数据库更新结果: " + updateResult);
            return url;
        } catch (Exception e) {
            System.err.println("医生头像上传异常: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("医生头像上传失败: " + e.getMessage());
        }
    }
}